package com.nf.health.app.adapter;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nf.health.app.Constants;
import com.nf.health.app.R;
import com.nf.health.app.activity.global.PictureActivity;
import com.nf.health.app.models.ConsultDetails;
import com.nf.health.app.utils.ActivityUtils;
import com.nf.health.app.utils.ImageLoaderUtils;
import com.nf.health.app.utils.LogUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 问题详情
 * 
 * @author jiax
 * 
 */
public class ConsultDetailsAdapter extends BaseAdapter {
	private static final int MESSAGE_TYPE_SENT_TXT = 0;
	private static final int MESSAGE_TYPE_RECV_TXT = 1;
	private static final int MESSAGE_TYPE_SENT_IMAGE = 2;
	private static final int MESSAGE_TYPE_RECV_IMAGE = 3;
	private Activity context;
	private List<ConsultDetails> listHealthy;

	public ConsultDetailsAdapter(Activity context,
			List<ConsultDetails> listHealthy) {
		this.context = context;
		this.listHealthy = listHealthy;
	}

	@Override
	public int getCount() {
		return listHealthy == null ? 0 : listHealthy.size();
	}

	@Override
	public ConsultDetails getItem(int position) {
		return listHealthy.get(position);
	}

	@Override
	public long getItemId(int id) {
		return id;
	}

	@Override
	public int getViewTypeCount() {
		return 4;
	}

	@Override
	public int getItemViewType(int position) {
		// if
		// (listHealthy.get(position).getUserid().equals(PreferenceHelper.getString(PreferenceHelper.USERID,
		// ""))) {
		// return MESSAGE_TYPE_SENT_TXT;
		// } else {
		// return MESSAGE_TYPE_RECV_TXT;
		// }
		if (getItem(position).getContent().startsWith(Constants.IMAG_PREFIX)) {
			// 图片类型
			if ("doctor".equals(getItem(position).getType())) {
				return MESSAGE_TYPE_RECV_IMAGE;
			} else {
				return MESSAGE_TYPE_SENT_IMAGE;
			}
		} else {
			if ("doctor".equals(getItem(position).getType())) {
				return MESSAGE_TYPE_RECV_TXT;
			} else {
				return MESSAGE_TYPE_SENT_TXT;
			}
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup viewGroup) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = createViewByMessage(position);
			switch (getItemViewType(position)) {
			case MESSAGE_TYPE_RECV_TXT:
				holder.timestamp = (TextView) convertView
						.findViewById(R.id.timestamp);
				holder.iv_userhead = (ImageView) convertView
						.findViewById(R.id.iv_userhead);
				holder.tv_chatcontent = (TextView) convertView
						.findViewById(R.id.tv_chatcontent);
				holder.prompt = (TextView) convertView
						.findViewById(R.id.prompt);
				break;
			case MESSAGE_TYPE_SENT_TXT:
				holder.timestamp = (TextView) convertView
						.findViewById(R.id.timestamp);
				holder.iv_userhead = (ImageView) convertView
						.findViewById(R.id.iv_userhead);
				holder.tv_chatcontent = (TextView) convertView
						.findViewById(R.id.tv_chatcontent);
				break;
			case MESSAGE_TYPE_RECV_IMAGE:
			case MESSAGE_TYPE_SENT_IMAGE:
				holder.timestamp = (TextView) convertView
						.findViewById(R.id.timestamp);
				holder.iv_userhead = (ImageView) convertView
						.findViewById(R.id.iv_userhead);
				holder.iv_sendPicture = (ImageView) convertView
						.findViewById(R.id.iv_sendPicture);
				break;
			}
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		if (getItemViewType(position) == MESSAGE_TYPE_RECV_TXT) {
			if (position == getCount() - 1) {
				holder.prompt.setVisibility(View.VISIBLE);
			} else {
				holder.prompt.setVisibility(View.GONE);
			}
		}

		holder.timestamp.setText(getItem(position).getUpdatetime());
		ImageLoaderUtils.getinstance(context).getImage(holder.iv_userhead,
				getItem(position).getHimg(), R.drawable.default_head);

		switch (getItemViewType(position)) {
		case MESSAGE_TYPE_RECV_TXT:
		case MESSAGE_TYPE_SENT_TXT:
			holder.tv_chatcontent.setText(getItem(position).getContent());
			break;
		case MESSAGE_TYPE_RECV_IMAGE:
		case MESSAGE_TYPE_SENT_IMAGE:
			try {
				final String url = getItem(position).getContent().split("=")[1];
				ImageLoaderUtils.getinstance(context).getImage(
						holder.iv_sendPicture, url, R.drawable.default_image);
				holder.iv_sendPicture
						.setOnClickListener(new View.OnClickListener() {
							@Override
							public void onClick(View v) {
								ArrayList<String> imgUrls = new ArrayList<String>();
								imgUrls.add(url);
								Bundle bundle = new Bundle();
								bundle.putStringArrayList("imgUrls", imgUrls);
								bundle.putInt("position", 0);
								// TODO
								 if(!TextUtils.isEmpty(url)&&url.startsWith("http://")){
									 bundle.putInt("type", PictureActivity.TYPE_NET_IMG);
								 }else {
									 bundle.putInt("type", PictureActivity.TYPE_LOCAL_IMG);
								 }
								 ActivityUtils.showActivity(context, PictureActivity.class, bundle);
							}
						});
			} catch (Exception e) {
				LogUtil.exception(e);
			}
			break;
		}
		return convertView;
	}

	private View createViewByMessage(int position) {
		switch (getItemViewType(position)) {
		case MESSAGE_TYPE_RECV_TXT:
			return View.inflate(context, R.layout.row_received_message, null);
		case MESSAGE_TYPE_SENT_TXT:
			return View.inflate(context, R.layout.row_sent_message, null);
		case MESSAGE_TYPE_RECV_IMAGE:
			return View.inflate(context, R.layout.row_received_picture, null);
		case MESSAGE_TYPE_SENT_IMAGE:
			return View.inflate(context, R.layout.row_sent_picture, null);
		default:
			return View.inflate(context, R.layout.row_sent_picture, null);
		}
	}

	class ViewHolder {
		ImageView iv_userhead;
		TextView tv_chatcontent;
		TextView prompt;
		TextView timestamp;
		ImageView iv_sendPicture;
	}
}
